Program price_and_turn
      character*6 ticker
      character*8 cusp,cusip
      integer(kind=8):: nshrs,nvalue,jvalue,ivol,numshrs(5500),ntot(5500),navail(5500),iperm(5500)
      dimension nep(5500),ntype(5500),jyr(5500),jmn(5500),jdy(5500),vwret(5500),retrn(5500),pctonloan(5500),jdate(5500),tfact(5000),favg(5500)
      dimension pctavail(5500),ivol(5500),ishrout(5500),price(5500),turno(5500),aturn(10),aprc(10),nms(100000),ndate(100000,52),factr(100000,52),avgfee(10) 
!      open(unit=10,file='e:\FIS_duration\episode_data_type_total.txt')
!      open(unit=11,file='e:\FIS_duration\splits.prn')
!      open(unit=99,file='e:\FIS_duration\price_turnover_fee_over_episode.txt')
      open(unit=10,file='e:\FIS_duration\episode_data_type_total_rep.txt')
      open(unit=11,file='e:\FIS_duration\splits.prn')
      open(unit=99,file='e:\FIS_duration\price_turnover_fee_over_episode_rep.txt')
      nms=0
      do 700 k=1,3587
      read(11,1100)kprm,kdate,fct
      write(6,1100)kprm,kdate,fct
1100  format(1x,i5,1x,i8,1x,f9.5)
      nms(kprm)=nms(kprm)+1
      ndate(kprm,nms(kprm))=kdate
      factr(kprm,nms(kprm))=fct
700   continue      
      
      
      iend=0
      i=0
1     i=i+1      
      read(10,1000,end=900)nep(i),ntype(i),jast,jyr(i),jmn(i),jdy(i),cusp,ticker,itickt,numshrs(i),age,favg(i),fmax,fmin,fstd,iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util,navail(i),ntot(i)
1000  format(1x,i4,1x,i1,1x,i8,1x,i4,2i2,1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13,1x,i12) 
      turno(i)=float(ivol(i))/(1000*float(ishrout(i)))
      jdate(i)=jyr(i)*10000+jmn(i)*100+jdy(i)
      if(nep(i).eq.nep(1)) go to 1
5     icnt=i-1
      if(icnt.lt.50) go to 100
      if(nms(iperm(1)).eq.0) go to 18
      do 16 nn=1,nms(iperm(1))
          write(6,6001)nep(1),nn
6001  format(1x,i4,1x,i2)          
          do 10 ii=1,icnt
              if(jdate(ii).ge.ndate(iperm(1),nn)) price(ii)=price(ii)*(1.+factr(iperm(1),nn))
10        continue
16    continue          
18    icntm=icnt-1
      do 20 i1=1,icntm
          ip=i1+1
          do 15 i2=ip,icnt
              if(turno(i1).le.turno(i2)) go to 15
              tmp1=turno(i1)
              tmp2=price(i1)
              tmp3=favg(i1)
              turno(i1)=turno(i2)
              price(i1)=price(i2)
              favg(i1)=favg(i2)
              turno(i2)=tmp1
              price(i2)=tmp2
              favg(i2)=tmp3
15        continue
20    continue
      tprice=0.
      do 30 ii=1,icnt
30    tprice=tprice+abs(price(ii))
      avgprc=tprice/float(icnt)
          rcnt=float(icnt)
          rcnt10=rcnt/10.
          icnt10=int(rcnt10)
          idif=icnt-10*icnt10
          aturn=0.
          aprc=0.
          avgfee=0.
          do 40 n=1,4
              ibg=(n-1)*icnt10+1
              iend=n*icnt10
              do 35 ii=ibg,iend
                  aturn(n)=aturn(n)+turno(ii)/(float(icnt10))
                  avgfee(n)=avgfee(n)+favg(ii)/(float(icnt10))
                  aprc(n)=aprc(n)+abs(price(ii))/(avgprc*float(icnt10))
35            continue
40        continue
          ibg=4*icnt10+1
          iend=5*icnt10+idif
          do 50 ii=ibg,iend
              aturn(5)=aturn(5)+turno(ii)/(float(icnt10)+float(idif))
              avgfee(5)=avgfee(5)+favg(ii)/(float(icnt10)+float(idif))
              aprc(5)=aprc(5)+abs(price(ii))/(avgprc*(float(icnt10)+float(idif)))
50    continue              
          do 60 n=6,10
              ibg=(n-1)*icnt10+1+idif
              iend=n*icnt10+idif
              do 55 ii=ibg,iend
                  aturn(n)=aturn(n)+turno(ii)/float(icnt10)
                  avgfee(n)=avgfee(n)+favg(ii)/(float(icnt10))
                  aprc(n)=aprc(n)+abs(price(ii))/(avgprc*float(icnt10))
55            continue
60        continue
      write(6,9900)nep(1),ntype(1),icnt,(aturn(n),n=1,10),(aprc(n),n=1,10),(avgfee(n),n=1,10)
      write(99,9900)nep(1),ntype(1),icnt,(aturn(n),n=1,10),(aprc(n),n=1,10),(avgfee(n),n=1,10)
9900  format(1x,i4,1x,i1,1x,i4,10(1x,f9.6),2x,10(1x,f10.5),2x,10(1x,f10.4))      
100   continue
      nep(1)=nep(i)
      ntype(1)=ntype(i)
      jyr(1)=jyr(i)
      jmn(1)=jmn(i)
      jdy(1)=jdy(i)
      retrn(1)=retrn(i)
      vwret(1)=vwret(i)
      ntot(1)=ntot(i)
      numshrs(1)=numshrs(i)
      navail(1)=navail(i)
      price(1)=price(i)
      turno(1)=turno(i)
      ivol(1)=ivol(i)
      ishrout(1)=ishrout(i)
      favg(1)=favg(i)
      i=1
      go to 1
900   continue
      iend=iend+1
      if(iend.eq.1) go to 5
      stop
end program price_and_turn